# DataFrame :二维数组 表格 n个横着的Series组成
import numpy as np
import pandas as pd

# 单一列表
print(pd.DataFrame([1, 2, 3, 4, 5]))

data = pd.DataFrame([[1, 2, 3], [5, 6, 7], [1, 1, 1], [2, 2, 2]], columns=['c1', 'c2', 'c3'])
print(data)

# 嵌套列表
data1 = [['Alex', 10], ['Bob', 12], ['Clarke', 13]]
print(pd.DataFrame(data1, columns=['Name', 'Age']))

# 字典列表
data2 = {'Name': ['Tom', 'Jack', 'Steve', 'Ricky'], 'Age': [28, 34, 29, 42]}
print(pd.DataFrame(data2))

# 列表嵌套字典
data3 = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
print(pd.DataFrame(data3))

# Series创建
data4 = {'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
         'two': pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}
df4 = pd.DataFrame(data4)
print(df4)

data5 = pd.DataFrame(np.random.randn(10, 2), index=[1, 6, 4, 2, 3, 5, 9, 8, 0, 7], columns=['col2', 'col1'])
print(data5)

# 取指定字段
print(df4.loc['b'], '-----')
print(df4.loc['a':'b'], '-----')
print(df4.loc['a':'b', 'one', 'two'], '-----')

# 取指定行索引
print(df4.iloc[2], '----')
# 切片
print(df4[2:4])

# 添加列 df['列']=值
df4['three'] = pd.Series([10, 20, 30], index=['a', 'b', 'c'])
print(df4, '---添加列---')

df4.insert(0, column='new', value=[1, 1, 1, 1])
print(df4, '---添加列---')

# 将已经存在的数据列做相加运算
df4['four'] = df4['one'] + df4['three']
print(df4)

# 删除列
del df4['one']
print(df4)

df4.pop('two')
print(df4)

df = pd.DataFrame([[1, 2], [3, 4]], columns=['a', 'b'])
df2 = pd.DataFrame([[5, 6], [7, 8]], columns=['a', 'b'])
df = df.append(df2)
print(df)
df = df.drop(0)
print(df)
